#include <stdio.h>
#include "my_uart.h"


USART_InitType USART_InitStructure;

UART_DATA_t uart_data = {0};

/***************************************
 * 串口重定向
********************************/
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE* f)
{
    USART_SendData(USARTx, (uint8_t)ch);
    while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET)
        ;

    return (ch);
}

/**
 * @brief  Configures the different system clocks.
 */
static void RCC_Configuration(void)
{
#ifdef USARTx    
    /* Enable GPIO clock */
    GPIO_APBxClkCmd(USARTx_GPIO_CLK, ENABLE);
    /* Enable USARTx Clock */
    USART_APBxClkCmd(USARTx_CLK, ENABLE);
#endif    
}

/*************************************************
 * @brief  Configures the nested vectored interrupt controller.
*************************************************/
static void uart_NVIC_cfg(void)
{
    NVIC_InitType NVIC_InitStructure;

    /* Configure the NVIC Preemption Priority Bits */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

#ifdef USARTx
    /* Enable the USARTx Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel            = USARTx_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd         = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
#endif

#ifdef USARTy    
    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel            = USARTy_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd         = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
#endif

#ifdef USARTz  
    /* Enable the USARTz Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel            = USARTz_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd         = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
#endif    
}


/**
 * @brief  Configures the different GPIO ports.
 */
static void GPIO_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;

    /* Initialize GPIO_InitStructure */
    GPIO_InitStruct(&GPIO_InitStructure);

#ifdef USARTx    
    /* Configure USARTx Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTx_TxPin;
	GPIO_InitStructure.GPIO_Pull      = GPIO_Pull_Up;
    GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Alternate = USARTx_Tx_GPIO_AF;
    GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure);
    
    /* Configure USARTx Rx as alternate function push-pull and pull-up */
    GPIO_InitStructure.Pin            = USARTx_RxPin;
    GPIO_InitStructure.GPIO_Pull      = GPIO_Pull_Up;
    GPIO_InitStructure.GPIO_Alternate = USARTx_Rx_GPIO_AF;
    GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure);
#endif    
}


/*********************************
 * 
 * 
******************************/
static void uart_param_cfg(void)
{
    /* USARTy and USARTz configuration ------------------------------------------------------*/
    USART_StructInit(&USART_InitStructure);
    USART_InitStructure.BaudRate            = 115200;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

#ifdef USARTx   
    /* Configure USARTx */
    USART_Init(USARTx, &USART_InitStructure);

    /* Enable USARTx Receive and Transmit interrupts */
    USART_ConfigInt(USARTx, USART_INT_RXDNE, ENABLE);
	//    USART_ConfigInt(USARTx, USART_INT_TXDE, ENABLE);	// 发送不用进入中断
    /* Enable the USARTx */
    USART_Enable(USARTx, ENABLE);
#endif    
}
/********************************************
 * 发送函数，阻塞发送，等待一个字节发送完毕后才发送下一个
 * 
****************************************/
void my_uart_send(uint8_t *p_buff, uint16_t len)
{
    uint16_t i = 0;

    for(i = 0; i < len; i++){
#ifdef USARTx 
        USART_SendData(USARTx, uart_data.tx_buff[i]);
		while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET)
			;
#endif        
    }
}

/********************************************
 * 回调函数
 * 
****************************************/
void my_uart_callback(void)
{
    if(uart_data.rx_cnt < 2){
        return;
    }
    if(uart_data.rx_buff[uart_data.rx_cnt -1] == 0x0a){

        memcpy(uart_data.tx_buff, uart_data.rx_buff, uart_data.rx_cnt);    
        my_uart_send(uart_data.tx_buff, uart_data.rx_cnt);
        uart_data.rx_cnt = 0;
    }
}

/*********************************
 * 
 * 整体初始化
******************************/
void my_uart_init(void)
{
    /* System Clocks Configuration */
    RCC_Configuration();

    /* Configure the GPIO ports */
    GPIO_Configuration();
    uart_param_cfg();
    uart_NVIC_cfg();

    /* Output a message on Hyperterminal using printf function */
    printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");

}
